package com.zsc.newbuds.service.impl;

import com.alibaba.fastjson.JSON;
import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.RecyclingArea;
import com.zsc.newbuds.mapper.AdminMapper;
import com.zsc.newbuds.mapper.RecyclingAreaMapper;
import com.zsc.newbuds.service.RecyclingAreaService;
import com.zsc.newbuds.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.naming.Name;
import java.sql.Connection;
import java.util.*;

/**
 * @author D
 */
@Service
public class RecyclingAreaServiceImpl implements RecyclingAreaService {

    @Autowired
    RecyclingAreaMapper recyclingAreaMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RedisUtil redisUtil;


    @Override
    public List<RecyclingArea> getAreaList() {
        List<RecyclingArea> areaList = new ArrayList<RecyclingArea>();
        areaList = recyclingAreaMapper.getAreaList();
        Collections.sort(areaList);
        return areaList;
    }


    @Override
    public void addRecyclingArea(RecyclingArea recyclingArea) {
        recyclingAreaMapper.addRecyclingArea(recyclingArea);
    }

    @Override
    public void updateRecyclingAreaState(String[] adminId, Integer areaId, String state, String[] workName) {
        System.out.println(areaId);
        System.out.println(state);
        System.out.println(Arrays.toString(adminId));
        System.out.println(Arrays.toString(workName));

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < workName.length; i++) {
            stringBuilder.append(workName[i]);
            if (i != workName.length - 1) {
                stringBuilder.append(",");
            }

        }
        System.out.println(stringBuilder);

        RecyclingArea recyclingArea = recyclingAreaMapper.getRecyclingArea(areaId);
        System.out.println(recyclingArea.getAssignPeople());
        if (!recyclingArea.getAssignPeople().equals("暂无")) {
            String name = recyclingArea.getAssignPeople() + "," + stringBuilder.toString();
            recyclingArea.setAssignPeople(name);
        } else {
            recyclingArea.setAssignPeople(stringBuilder.toString());
        }
        System.out.println(recyclingArea.getAssignPeople());
        String workArea = recyclingArea.getProvince() + recyclingArea.getCity() + recyclingArea.getNeighbourhood();

        int times = recyclingArea.getRecoveryTimes();
        times = times + 1;
        recyclingArea.setRecoveryTimes(times);
        recyclingArea.setState(state);
        recyclingAreaMapper.updateRecyclingAreaState(areaId, state, times, recyclingArea.getAssignPeople());

        for (String id : adminId) {
            adminMapper.updateWorkSate(Integer.parseInt(id), workArea);
        }

    }


    /**
     * Object 转 List<T></T>
     *
     * @param obj   Object
     * @param clazz 类型
     * @param <T>   类型
     * @return List<T>
     */
    public <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    /**
     * 每晚更新分配状态
     */
    @Override
    @Scheduled(cron = "0 59 23 * * ? ")
    public void updatePerson() {
        adminMapper.updateSate("未分配");
        recyclingAreaMapper.updateState("未分配", "暂无");
    }


}
